<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>Edit Account</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
</head>

<body>



<?php
session_start();


	
	$con = mysql_connect("localhost","root","") or die ("Could not connect to DB!");
	mysql_select_db ("web project",$con) or die ("could not select DB!");
	
	$search = "SELECT * FROM members WHERE Mem_ID = '".$_SESSION['id']."'";
	$result = mysql_query($search);
	$num = mysql_num_rows($result);
	
	$searchs = "SELECT * FROM credit_info WHERE Member_Id = '".$_SESSION['id']."'";
	$results = mysql_query($searchs);
	$nums = mysql_num_rows($results);
	
	mysql_close($con); 
		

	
								
	
	

//--------------------------------------------------------------------------------------------------------------------------------------------//
	$i = 0;
	
	while($i < $num)
	{
		$afname = mysql_result($result,$i,"First_Name");
		$alname = mysql_result($result,$i,"Last_Name");
		$apassword = mysql_result($result,$i,"Password");
		$aDOB = mysql_result($result,$i,"DOB");
		$aadd1 = mysql_result($result,$i,"Address1");
		$aadd2 = mysql_result($result,$i,"Address2");
		$aemail = mysql_result($result,$i,"email");
		
		while($i < $nums)
		{
		$accard = mysql_result($results,$i,"creditcard");
		$i++;
		}
		
		echo "<form action='editaccount.php' method='post'>
	<table>
    	<tr>
        	<td>First Name:</td>
            <td><input type='Text' name='firstname' style='width:200px' value= '$afname' /></td>
         </tr>
         <tr>
        	<td>Last Name:</td>
            <td><input type='Text' name='lastname' style='width:200px' value='$alname' /></td>
         </tr>
         <tr>
        	<td>Date of Birth:</td>
            <td><select name='dob_day' >
            	<option value='1'>1</option>
                <option value='2'>2</option>
                <option value='3'>3</option>
                <option value='4'>4</option>
                <option value='5'>5</option>
                <option value='6'>6</option>
                <option value='7'>7</option>
                <option value='8'>8</option>
                <option value='9'>9</option>
                <option value='10'>10</option>
                <option value='11'>11</option>
                <option value='12'>12</option>
                <option value='13'>13</option>
                <option value='14'>14</option>
                <option value='15'>15</option>
                <option value='16'>16</option>
                <option value='17'>17</option>
                <option value='18'>18</option>
                <option value='19'>19</option>
                <option value='20'>20</option>
                <option value='21'>21</option>
                <option value='22'>22</option>
                <option value='23'>23</option>
                <option value='24'>24</option>
                <option value='25'>25</option>
                <option value='26'>26</option>
                <option value='27'>27</option>
                <option value='28'>28</option>
                <option value='29'>29</option>
                <option value='30'>30</option>
                <option value='31'>31</option></select><select name='dob_month' >
                                                        <option value='1'>January</option>
                                                        <option value='2'>February</option>
                                                        <option value='3'>March</option>
                                                        <option value='4'>April</option>
                                                        <option value='5'>May</option>
                                                        <option value='6'>June</option>
                                                        <option value='7'>July</option>
                                                        <option value='8'>August</option>
                                                        <option value='9'>September</option>
                                                        <option value='10'>October</option>
                                                        <option value='11'>November</option>
                                                        <option value='12'>December</option></select> <select name='dob_year' >
                                                        									<option value ='1980'>1970</option>
                                                                                            <option value ='1981'>1971</option>
                                                                                            <option value ='1982'>1972</option>
                                                                                            <option value ='1983'>1973</option>
                                                                                            <option value ='1984'>1974</option>
                                                                                            <option value ='1985'>1975</option>
                                                                                            <option value ='1986'>1976</option>
                                                                                            <option value ='1987'>1977</option>
                                                                                            <option value ='1988'>1978</option>
                                                                                            <option value ='1989'>1979</option>
                                                                                            <option value ='1980'>1980</option>
                                                                                            <option value ='1981'>1981</option>
                                                                                            <option value ='1982'>1982</option>
                                                                                            <option value ='1983'>1983</option>
                                                                                            <option value ='1984'>1984</option>
                                                                                            <option value ='1985'>1985</option>
                                                                                            <option value ='1986'>1986</option>
                                                                                            <option value ='1987'>1987</option>
                                                                                            <option value ='1988'>1988</option>
                                                                                            <option value ='1989'>1989</option>
                                                                                            <option value ='1990'>1990</option>
                                                                                            <option value ='1991'>1991</option>
                                                                                            <option value ='1992'>1992</option>

                                                                                            <option value ='1993'>1993</option>
                                                                                            <option value ='1994'>1994</option>
                                                                                            <option value ='1995'>1995</option>
                                                                                            <option value ='1996'>1996</option>
                                                                                            <option value ='1997'>1997</option>
                                                                                            <option value ='1998'>1998</option>
                                                                                            <option value ='1999'>1999</option>
                                                                                            <option value ='2000'>2000</option>
                                                                                            </select>
            </td>
         </tr>
         
         <tr>
         	<td>Email:</td>
            <td><input type='text' name='email' style='width:200px' value='$aemail' /></td>
         </tr>
         <tr>
         	<td>Address 1:</td>
            <td><input type='text' name='Address1' style='width:200px'  value='$aadd1'/></td>
    	</tr>
        <tr>
         	<td>Address 2:</td>
            <td><input type='text' style='width:200px' name='Address2' value='$aadd2'/></td>
    	</tr>
        <tr>
         	<td>Credit card number:</td>
            <td><input type='text' style='width:200px' name='ccard' value='$accard' /></td>
    	</tr>
        <tr>
         	<td>Card Type</td>
            <td><select name='card_type' >
            	<option value='NULL'>Select card type</option>
                <option value ='visa'>Visa</option>
                <option value ='discover'>Discover</option>
                <option value ='mastercard'>Mastercard</option>
                </select><img src='../Resources/ccards.png' width='78' height='18' alt='ccard' /></td>
    	</tr>
         <tr>
         	<td>Password:</td>
            <td><input type='password' style='width:200px' name='pin' /></td>
    	</tr>
        <tr>
         	<td>Repeat Password:</td>
            <td><input type='password' style='width:200px' name='pin2' /></td>
    	</tr>
    </table>
 	<input type='submit' value='Register' name='submitedit' /><a href='Index.php'><input type='button' value ='Cancel' /></a>
</form>";
		$i++;
	}

if (isset($_POST['submitedit']))
{	
if(!@$_POST['firstname']|| !@$_POST['lastname'] || !@$_POST['dob_day'] || !@$_POST['dob_month'] || !@$_POST['dob_year'] || !@$_POST['email'] || !@$_POST['Address1'] || !@$_POST['Address2'] || !@$_POST['pin'] || !@$_POST['pin2'] || !@$_POST['ccard'] || !@$_POST['card_type'])
	
	{
		die("<strong>One or more critical fields left blank</strong>");
	}
	else
	{
		if($_POST['pin']!= $_POST['pin2'])
		{
			die("Passwords do not match");
		}
		else
		{
			if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["firstname"]) === 0 | preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["lastname"]) === 0)
			{
				die("Name can only be letters dashes or spaces and cannot start with a dash");
			}
			else
			{
				
				
					if(preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["pin"])=== 0)
					{
						die("Password not strong enough");
					}
					else
					{
						if(preg_match("/^[\d]{16}+$/",$_POST['ccard'])===0)
						{
							die("Credit Card Number can only contain numbers");

						}
						else
						{
							if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0)
							{
								die("email address not valid");
							}
							else
							{
	
									@$ufirstname =$_POST['firstname'];
									@$ulastname = $_POST['lastname'];
									//$mem_ID= $firstname[0]. $lastname[0]. rand(1000,9999);
									@$uday = $_POST['dob_day'];
									@$umonth= $_POST['dob_month'];
									@$uyear= $_POST['dob_year'];
									@$udob=  $uyear."-" .$umonth."-".$uday;
									
									$uaddress1 = $_POST['Address1'];
									$uaddress2 = $_POST['Address2'];
									$uemail = $_POST['email'];
									$uccard = $_POST['ccard'];
									
									@$upassword = $_POST['pin'];
									
									$con = mysql_connect("localhost","root","") or die ("Could not connect to DB!");
									mysql_select_db ("web project",$con) or die ("could not select DB!");
	
								$id = $_SESSION['id'];
																		
								$uquery = "UPDATE members SET First_Name = '$ufirstname',
									 						Last_Name = '$ulastname',
															Password = '$upassword', 
															DOB = '$udob',
															Address1 = '$uaddress1',
															Address2 = '$uaddress2',
															email = '$uemail'
									 			WHERE Mem_ID = '$id'";
												
											mysql_close($con); 	
											
								$con = mysql_connect("localhost","root","") or die ("Could not connect to DB!");
								mysql_select_db ("web project",$con) or die ("could not select DB!");
								
								mysql_query($uquery);
								
								$usql = "UPDATE `credit_info` SET `creditcard` = ['$uccard'],
																`password` = ['$upassword'],
																`address` = ['$uaddress1'], 
															WHERE `Member_Id` = '$id' ";
												
								mysql_query($usql);	                  
										
										mysql_close($con); 
										echo "Profile updated successfully";
										echo "<meta http-equiv='refresh' content='6;url=index.php'>"; 
										die("<br>wait.....");
	
									
							}
						}
					}
				
			}
		}
	}
}	
	
?>


</body>
</html>